var util = require('../../utils/util.js');
var api = require('../../config/api.js');
const pay = require('../../services/pay.js');
var that;
var app = getApp();
Page({
  data: {
    status: false,
    searching:false,
    times:3,
    orderId: 0
  },
  onLoad: function (options) {
    that = this;
    // 页面初始化 options为页面跳转所带来的参数
    that.setData({
      orderId: options.orderId || 0
    })
    that.searchPayRlt();
  },
  searchPayRlt(){ //查询付款结果
    wx.showLoading({
      mask: true,
      title: '结果查询中...',
    })
    app.postPureForm(api.SearchPayRlt, { id: that.data.orderId, retry: 3, isAllow:true }).then(rsp => {
      if (!rsp.data || (rsp.data && rsp.data.rentStatus == "unpaid")){
        that.data.times--;
        if (that.data.times>0){
          setTimeout(function () {
            that.searchPayRlt();
          }, 10000);
        }else{
          wx.redirectTo({
            url: '/pages/bill/list/list',
          })
        }
      }else{
        that.setData({
          status: rsp.data.rentStatus == "paySuccess",
          searching:true,
        });
        wx.hideLoading();
      }
    }).catch((err) => {
      wx.hideLoading();
      console.log(err, err);
    });
  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  onPullDownRefresh(){
    wx.stopPullDownRefresh();
  },
  payOrder() {
    pay.payOrder(parseInt(that.data.orderId)).then(res => {
      that.searchPayRlt();
    }).catch(res => {
      util.toast.fail('支付失败');
    });
  }
})